<?php
/**
 * @uses 		web接受数据
 * @author 		Duan
 * @version 	1.0
 * @copyright	Yihuan	
 */

namespace 	Myfuyou\Controller;

use 		Common\Controller\BaseController;

class ReceiveController extends BaseController
{

	private $token 	= '';    	//token值
 
	private $key 	= ''; 		//医院key值

	function __construct()
	{	
		parent::__construct();
		$this->key 		= C('KEY');
		$hospitalId 	= $_POST['hospitalId'];
		$this->token 	= C('TOKEN');
		$this->key != $hospitalId && $this->jsonResult(202);
		$this->token || $this->jsonResult(203); 		//缺少token
		$this->checkAuth(); 							//检查签名
		$method = ACTION_NAME;
		$code 	= D('Common/Receive')->$method();
		$this->jsonResult($code);
	}
	/**
	 * @uses 	权限验证
	 * @return 	void
	 */
	private function checkAuth()
	{	
		$sign 	= $this->middelEnCodeSign($_POST);
		$sign != $_POST['sign'] && $this->jsonResult(201);
	}
	/**
	 * @uses 	签名生成
	 * @param 	签名参数
	 * @return  string 签名
	 */
	private function middelEnCodeSign($params) 
	{
		ksort($params);
		$preString 	= '';
		$time 		= $params['time'];
		$hospitalId = $params['hospitalId'];
		foreach ($params as $k => $v) {
			if ($v && !in_array($k , ['time' , 'sign'])) {
				$preString .= $k.'='.$v;
			}
		}
		$keyString 	= $this->token;
		return sha1($preString.$keyString.$time);
	}

}